<template>
	<view>
		<view class="infoItem">
			<view class="infoItem-title">旧密码：</view>
			<input :password="!isChecked" v-model="oldPwd" placeholder="请输入旧密码" placeholder-class="inputDefault" />
		</view>
		<view class="infoItem">
			<view class="infoItem-title">新密码：</view>
			<input :password="!isChecked" v-model="newPwd" placeholder="请输入新密码" placeholder-class="inputDefault" />
		</view>
		<view class="infoItem">
			<view class="infoItem-title">确认新密码：</view>
			<input :password="!isChecked" v-model="newPwdConfirm" placeholder="请再次输入新密码" placeholder-class="inputDefault" />
		</view>
		<view class="radioGroup" @tap="isChecked =! isChecked">
			<radio color="#E6B873" :checked="isChecked" /><text>显示密码</text>
		</view>
		<view class="saveBtn" @tap="changePwd">确认修改</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				oldPwd: '', //原密码
				newPwd: '', //新密码
				newPwdConfirm: '', //确认新密码
				isChecked: false, //是否显示密码
			}
		},
		methods: {
			// 修改密码
			changePwd() {
				// 判断数据是否全部输入完毕
				if (!this.oldPwd) {
					this.util.showDialog('请输入旧密码')
					return false
				}
				if (!this.newPwd) {
					this.util.showDialog('请输入新密码')
					return false
				}
				if (!this.newPwdConfirm) {
					this.util.showDialog('请再次确认新密码')
					return false
				}
				var data = {
					'old_pwd': this.oldPwd,
					'pwd': this.newPwd
				}
				var reg = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/; //密码验证规则
				if (!reg.test(this.newPwd)) {
					this.util.showDialog('新密码格式不正确')
				} else {
					if (this.newPwd != this.newPwdConfirm) {
						this.util.showDialog('您两次输入的密码不一致')
					} else {
						this.$api.apiPost('user/currency.basic/UpdatePas', data).then(res => {
							if (res.data.code == 200) {
								this.util.showDialog('密码修改成功')
								this.$api.apiPost('user/currency.basic/UserLoginOut').then(res => {
									return
								})
								uni.setStorageSync('token', '');
								setTimeout(function() {
									uni.reLaunch({
										url: '/pages/index/index'
									})
								}, 3000);
							} else {
								this.util.showDialog(res.data.msg)
							}
						})
					}
				}
			}
		}
	}
</script>

<style>
	.infoItem-title {
		font-weight: bold;
	}
</style>
